package com.yicong.ants.ui.me.recommend;

import android.view.View;

import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.MyRecSummaryActivityBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.coin.ContributionRecordActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的推荐
 *
 * @author cchao
 * @version 2019-05-28.
 */
public class MyTeamActivity extends BaseTitleBarActivity<MyRecSummaryActivityBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.my_rec_summary_activity;
    }

    @Override
    protected void initEventAndData() {
        mDataBind.setClick(this);
        setTitleBarVisible(false);
        AppHelper.transStatus(getWindow());
        setTitleText("我的推荐");

/*        addTitleMenuItem(R.drawable.icon_question_mark, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //
            }
        });*/

        onLoadData();
    }

    @Override
    protected void onLoadData() {
        switchView(LOADING);
        addSubscribe(RetrofitHelper.getApis().summary()
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    switchView(NET_ERROR);
                    return;
                }
                switchView(CONTENT);
                mDataBind.setBean(respBean.getData());
            }, RxHelper.getSwitchErrorConsumer(this)));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            case R.id.search_child:
                if (StringHelper.isEmpty(mDataBind.editChildUid.getText())) {
                    showToast("请输入用户蚁丛号");
                    return;
                }
                showProgress();
                UiHelper.setVisibleElseGone(mDataBind.childInfo, false);

                Map<String, String> map = new HashMap<>();
                map.put("child_uid", mDataBind.editChildUid.getText().toString());
                addSubscribe(RetrofitHelper.getApis().searchChild(map)
                    .compose(RxHelper.toMain())
                    .subscribe(respBean -> {
                        hideProgress();
                        if (respBean.isCodeFail()) {
                            showToast(respBean.getMsg());
                            return;
                        }
                        mDataBind.setChild(respBean.getData());
                        if (respBean.getData().getAuth_status() == 0) {
                            mDataBind.childAuthStatus.setTextColor(UiHelper.getColor(R.color.red_dark_primary));
                        } else {
                            mDataBind.childAuthStatus.setTextColor(UiHelper.getColor(R.color.green_primary));
                        }
                    }, RxHelper.getHideProgressConsumer(this)));
                break;
            case R.id.child_close:
                UiHelper.setVisibleElseGone(mDataBind.childInfo, false);
                break;
            case R.id.my_contribution_field:
                Router.turnTo(mContext, ContributionRecordActivity.class).start();
                break;
            case R.id.direct_detail:
                Router.turnTo(mContext, MyDirectListActivity.class)
                    .putExtra(Constants.Extra.IsDirect, 1)
                    .start();
                break;
            case R.id.indirect_detail:
                Router.turnTo(mContext, MyInDirectListActivity.class)
                    .putExtra(Constants.Extra.IsDirect, 0)
                    .start();
                break;
            case R.id.contribution_rule:
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_rule_contribution");
                break;
        }
    }
}
